package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Admin;
import entity.LayRequest;
import mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class AdminServiceImpl  implements AdminService{
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public boolean insert(Admin admin) {
        return adminMapper.insert(admin)>0?true:false;
    }

    @Override
    public Admin selectByAccount(String account) {
        return adminMapper.selectByAccount(account);
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<Admin> selectByPage(Map<String, Object> map) {
        int pageNum=map.get("pageNum")==null?1:Integer.valueOf(map.get("pageNum").toString());
        int pageSize=map.get("pageSize")==null?0:Integer.valueOf(map.get("pageSize").toString());
        //1.设置当前页数和每页记录条数
        PageHelper.startPage(pageNum,2);
        //2.执行查询
        List<Admin> admins=adminMapper.selectByMap(map);
        //
        PageInfo<Admin> adminPageInfo=new PageInfo<>(admins);
        return adminPageInfo;
    }

    @Override
    public boolean delete(Integer id) {
        return adminMapper.delete(id)>0?true:false;
    }

    @Override
    public boolean update(Admin admin) {
        admin.setUpdatetime(new Date());
        return adminMapper.update(admin)>0?true:false;
    }
    @Override
    public boolean updateHeadPic(Admin admin){
        return adminMapper.updateHeadPic(admin)>0?true:false;
    }

    @Override
    public PageInfo<Admin> selectByPage(LayRequest layRequest) {
        PageHelper.startPage(layRequest.getPage(),layRequest.getLimit());
        List<Admin> admins=adminMapper.selectByMap(layRequest.getParams());
        PageInfo<Admin> adminPageInfo=new PageInfo<>(admins);
        return adminPageInfo;
    }

}
